package com.lz.ht.controller;

import com.lz.ht.base.BaseController;
import com.lz.ht.model.Customer;
import com.lz.ht.model.Customers;
import com.lz.ht.page.PageModel;
import com.lz.ht.result.Result;
import com.lz.ht.service.CustomerService;
import com.lz.ht.service.CustomersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName: CoutomersController
 * @Description:
 * @Author: CHonghao
 * @Date: 2021/7/8 11:25
 * @Version: V1.0
 **/

@Slf4j
@Controller
public class CoutomersController extends BaseController {

    @Autowired
    private CustomersService customersServiceImpl;


    @RequestMapping(value = "/customers/list",method = {RequestMethod.GET})
    public String customers_list()throws Exception{
        return "customers/customers_list";
    }



    @RequestMapping(value = "/customers/list",method = {RequestMethod.POST})
    @ResponseBody
    public PageModel list(Customers customers, PageModel<Customers> page)throws Exception{
        page.init();
        List<Customers> list = customersServiceImpl.findPageList(page,customers);
        long count = customersServiceImpl.findCount(customers);
        page.packData(count,list);
        return page;
    }

    @RequestMapping(value = "/customers/add",method = {RequestMethod.GET})
    public String addInit(Customers customers,Model model){
        return "customers/customers_add";
    }

    @RequestMapping(value = "/customers/add",method = {RequestMethod.POST})
    @ResponseBody
    public Result add(Customers customers){
        customersServiceImpl.add(customers);
        return Result.genSuccessResult();
    }

    @RequestMapping(value = "/customers/update",method = {RequestMethod.GET})
    public String updateInit(Customers customers, Model model){
        customers = customersServiceImpl.findById(customers.getCid());
        model.addAttribute("customers",customers);
        return "customers/customers_update";
    }

    @RequestMapping(value = "/customers/update",method = {RequestMethod.POST})
    @ResponseBody
    public Result update(Customers customers){
        customersServiceImpl.updateById(customers);
        return Result.genSuccessResult();
    }
    @RequestMapping(value = "/customers/delete",method = {RequestMethod.POST})
    @ResponseBody
    public Result delete(Customers customers){
        customersServiceImpl.deleteById(customers.getCid());
        return Result.genSuccessResult();
    }

}
